package com.javabasic.algorithm.sword_of_offer;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/1/7 12:02 上午
 */
public class 剑指_Offer_05_替换空格 {

    /**
     * 解法一：使用Java内置函数
     * @param s
     * @return
     */
    public String replaceSpace(String s) {
        String result = s.replaceAll(" ", "%20");
        return result;
    }


    /**
     * 遍历+替换
     * @param s
     * @return
     */
    public String replaceSpace2(String s) {
        int len = s.length();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == ' ') {
                result.append("%20");
            } else {
                result.append(s.charAt(i));
            }
        }
        return result.toString();
    }
}
